package com.ruoyi.qywx.getway.api;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.qywx.getway.entity.QyDept;
import com.ruoyi.qywx.getway.entity.QyContact;
import com.ruoyi.qywx.getway.entity.QyTagGroup;
import com.ruoyi.qywx.getway.request.TxRequest;
import com.ruoyi.qywx.getway.request.TxResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : hcl
 * @Date : 2023/8/7
 * 部门api
 */
@Slf4j
public class SyncAppApi {

    /**
     * 获取部门列表
     * <p>
     * https://developer.work.weixin.qq.com/document/path/90208
     */
    public static List<QyDept> departmentsGet(Meta meta) {
        TxResponse response = TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .get("/cgi-bin/department/list");
        if (response.isSuccess()) {
            return response.getJson().getJSONArray("department").toList(QyDept.class);
        }
        return null;
    }

    /**
     * 获取单个部门详情
     *
     * @param deptId https://developer.work.weixin.qq.com/document/path/95407
     */
    public static QyDept departmentGet(Meta meta, Long deptId) {
        TxResponse response = TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .get("/cgi-bin/department/get?id=" + deptId);
        if (response.isSuccess()) {
            log.info(response.getJson().toString());
            return response.getJson().toBean(QyDept.class);
        }
        return null;
    }

    /**
     * 获取单个成员详情
     *
     * @param userId https://developer.work.weixin.qq.com/document/path/91335#15232
     */
    public static QyContact userGet(Meta meta, String userId) {
        TxResponse response = TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .get("/cgi-bin/user/get?userid=" + userId);
        if (response.isSuccess()) {
            return response.getJson().toBean(QyContact.class);
        }
        return null;
    }

    /**
     * 部门下所有的员工
     *
     * @param id   部门id
     * @param meta meta信息
     *             https://developer.work.weixin.qq.com/document/path/90344
     */
    public static List<QyContact> departmentUsers(Long id, Meta meta) {
        TxResponse response = TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .get("/cgi-bin/user/list?department_id=" + id);
        if (response.isSuccess()) {
            JSONArray userlist = response.getJson().getJSONArray("userlist");
            if (CollectionUtils.isNotEmpty(userlist)) {
                return userlist.toList(QyContact.class);
            }
        }
        return Collections.emptyList();
    }

    /**
     * 获取标签
     * https://developer.work.weixin.qq.com/document/path/92117#%E8%8E%B7%E5%8F%96%E4%BC%81%E4%B8%9A%E6%A0%87%E7%AD%BE%E5%BA%93
     */
    public static List<QyTagGroup> qywxTagsPost(Meta meta, List<String> tagWxIds, List<String> tagGroupWxIds) {
        Map<String, Object> body = new HashMap<>();
        if (CollectionUtils.isNotEmpty(tagWxIds)) {
            body.put("tag_id", tagWxIds);
        }
        if (CollectionUtils.isNotEmpty(tagGroupWxIds)) {
            body.put("group_id", tagGroupWxIds);
        }
        TxResponse txResponse = TxRequest.getInstance(meta.getCorpid(), meta.getAppSecret())
                .post("/cgi-bin/externalcontact/get_corp_tag_list", body);
        if (!txResponse.isSuccess()) {
            if (log.isDebugEnabled()) {
                log.debug(txResponse.getJson().toString());
            }
            return null;
        }
        return txResponse.getJson()
                .getJSONArray("tag_group")
                .toList(QyTagGroup.class);
    }
}
